from  . import Base
from sqlalchemy import Column, BigInteger, String, DateTime,Boolean
from sqlalchemy.sql import func
import uuid,string,random
from utils.snowflake.snowflake import Snowflake
from settings import SNOWFLAKE_DATACENTER_ID,SNOWFLAKE_WORKER_ID
from sqlalchemy_serializer import SerializerMixin

snowflake = Snowflake(datacenter_id=SNOWFLAKE_DATACENTER_ID,worker_id=SNOWFLAKE_WORKER_ID)

def generate_username():
    code="".join(random.sample(string.digits,6))
    return '一枚_'+code

def generate_snowflake_id():
    new_id= snowflake.get_id()
    return int(new_id)


class User(Base,SerializerMixin):
    __tablename__='user'
    serialize_only=['id','username','mobile','avatar','is_active','is_staff','last_login']
    id=Column(BigInteger,primary_key=True,index=True,autoincrement=False,default=generate_snowflake_id)
    username=Column(String(20),default=generate_username)
    mobile=Column(String(11),unique=True,index=True)
    password=Column(String(300),nullable=True)
    email=Column(String(50),unique=True,nullable=True)
    avatar=Column(String(200),nullable=True)
    is_active=Column(Boolean,default=True)

    is_staff=Column(Boolean,default=False)
    create_time=Column(DateTime(timezone=True),server_default=func.now())
    last_login=Column(DateTime(timezone=True),nullable=True)
